angular.module('starter')
  .config(function($stateProvider) {
    $stateProvider.state('checkTableRecord', {
      url: '/checkTableRecord/:tableType/:status/:type',
      templateUrl: 'templates/checkTableRecord/checkTableRecord.html',
      controller: 'checkTableRecordCtrl',
      controllerAs: 'checkTableRecordCtrl'
    })
  })
  .controller('checkTableRecordCtrl', function($scope, $http, $stateParams) {
    var _this = $scope;

    //查询条件
    this.queryData = {
      status: $stateParams.status ? $stateParams.status : null, //0草稿,1未验收,2 验收未通过,3验收通过
      // tenderId: "", //所属标段
      // workPoint: "", //作业点
      type: $stateParams.type ? $stateParams.type : 0, //默认所有,1:由我创建 2:由我验收
      tableType: $stateParams.tableType ? $stateParams.tableType : null, //操作表格类型
      startTime: "", //日期筛选
      startTimeDate: "", //日期筛选
      title: "", //模糊查询条件
    };

    this.queryTableList = function() {
      if (this.queryData.startTimeDate && this.queryData.startTimeDate != "") {
        this.queryData.startTime = $scope.dateToStr(this.queryData.startTimeDate, 'yyyy-MM-dd');
      }
      console.log(this.queryData);
      $http.post("qss/workbench/queryWorkBenchList", this.queryData).success(function(res) {
        if (res.success) {
          _this.checkTableRecordCtrl.checkTableList = res.rows;
        }
      });
    };

    //初始化操作，根据表格所属，表格的状态，来确定表格标题以及可操作状态
    this.initPage = function() {
      this.operateStatus = 0; //0:查看 1：可编辑删除 2：可验收
      this.title = this.getTableTitle();

      if (this.queryData.status == 0 && this.queryData.type == 1) {
        this.title = "由我创建--草稿箱";
        this.operateStatus = 1;
      }
      if (this.queryData.status == 1 && this.queryData.type == 1) {
        this.title = "由我创建--未验收";
        this.operateStatus = 0;
      }
      if (this.queryData.status == 2 && this.queryData.type == 1) {
        this.title = "由我创建--未通过";
        this.operateStatus = 1;
      }
      if (this.queryData.status == 3 && this.queryData.type == 1) {
        this.title = "由我创建--通过";
        this.operateStatus = 0;
      }
      if (this.queryData.status == 1 && this.queryData.type == 2) {
        this.title = "由我验收--未验收";
        this.operateStatus = 2;
      }
      if (this.queryData.status == 2 && this.queryData.type == 2) {
        this.title = "由我验收--未通过";
        this.operateStatus = 0;
      }
      if (this.queryData.status == 3 && this.queryData.type == 2) {
        this.title = "由我验收--通过";
        this.operateStatus = 0;
      }

      this.queryTableList();
    }

    //根据表格类型获取表格标题
    this.getTableTitle = function() {
      var title = "";
      if (this.queryData.tableType == null) {
        return "";
      };
      switch (parseInt(this.queryData.tableType)) {
        case 4:
          title = "表格4";
          break;
        case 5:
          title = "表格5";
          break;
        case 6:
          title = "表格6";
          break;
        case 7:
          title = "表格7";
          break;
        case 8:
          title = "表格8";
          break;
        case 9:
          title = "表格9";
          break;
        case 10:
          title = "表格10";
          break;
      }
      return title;
    };

    //新增或者修改表格，存在tableId，存在则修改，无则新增
    this.modifyTable = function(type, recordId) {
      var TType = type ? type : this.queryData.tableType;
      switch (parseInt(TType)) {
        case 4:
          _this.goNewPage('addTable4', {
            recordId: recordId
          });
          break;
        case 5:
          _this.goNewPage('addTable5', {
            recordId: recordId
          });
          break;
        case 6:
          _this.goNewPage('addTable6', {
            recordId: recordId
          });
          break;
        case 7:
          _this.goNewPage('addTable7', {
            recordId: recordId
          });
          break;
        case 8:
          _this.goNewPage('addTable8', {
            recordId: recordId
          });
          break;
        case 9:
          _this.goNewPage('addTable9', {
            recordId: recordId
          });
          break;
        case 10:
          _this.goNewPage('addTable10', {
            recordId: recordId
          });
          break;
      }
    };

    /*
     * 查看表格信息
     * type:表格类型，tableId，表格id
     */
    this.checkTable = function(type, recordId) {

      switch (parseInt(type)) {
        case 4:
          _this.goNewPage('checkTable4', {
            recordId: recordId
          });
          break;
        case 5:
          _this.goNewPage('checkTable5', {
            recordId: recordId
          });
          break;
        case 6:
          _this.goNewPage('checkTable6', {
            recordId: recordId
          });
          break;
        case 7:
          _this.goNewPage('checkTable7', {
            recordId: recordId
          });
          break;
        case 8:
          _this.goNewPage('checkTable8', {
            recordId: recordId
          });
          break;
        case 9:
          _this.goNewPage('checkTable9', {
            recordId: recordId
          });
          break;
        case 10:
          _this.goNewPage('checkTable10', {
            recordId: recordId
          });
          break;
      }
    };

    this.deleteTable = function(type, recordId) {
      _this.showConfirm("提示", "是否删除该表格", function() {

      });
    };

    this.approve = function(type, recordId) {
      _this.showConfirm("提示", "是否验收通过该表格", function() {

      });
    };


    this.initPage();

  });